package leetcode_1501_1600;

import java.util.Arrays;

public class LeeCode_1505 {
    public static void main(String[] args) {
        System.out.println(minInteger("4321", 4));
        System.out.println(minInteger("100", 1));
    }
    private static String minInteger(String num, int k) {
        if (k == 0)
            return num;
        int  n = num.length();
        if (k < n * (n - 1)){
            for (char c = '0'; c <= '9'; c++){
                int index = num.indexOf(c);
                if (index >= 0){
                    if (index <= k){
                        return c + minInteger(num.substring(0, index) + num.substring(index + 1), k - index);
                    }
                }
            }
        }else {
            char[] s = num.toCharArray();
            Arrays.sort(s);
            return new String(s);
        }
        return "";
    }
}